package p3as.cliente;

import java.awt.Graphics;

public class Brick implements Drawable, CollisionListener {

    private int x;
    private int y;
    private int width;
    private int heigth;
    private Score score;

    public Brick(int x, int y, int width, int heigth) {
        this.x = x;
        this.y = y;
        this.width = width;
        this.heigth = heigth;
    }

    @Override
    public void addChild(Drawable drawable) {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    public void removeChild(Drawable drawable) {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    public Drawable getChild(int index) {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    public void render(Graphics graphics) {
        graphics.fillRect(x, y, width, heigth);
    }

    @Override
    public int getX() {
        return x;
    }

    @Override
    public int getY() {
        return y;
    }

    @Override
    public int getWidth() {
        return width;
    }

    @Override
    public int getHeigth() {
        return heigth;
    }

    @Override
    public int collision(Drawable drawable) {
        Ball ball = (Ball) drawable;
        if(x <= ball.getX() && ball.getX() <= x+width){
            if(y+heigth+2 >= ball.getY() && ball.getY() >= y+heigth -2){
                ball.setDy(ball.getDy()*(-1));
                return getScore().getScore();
            }
            else if(y+2 >= ball.getY()+ball.getHeigth() && ball.getY()+ball.getHeigth() >= y-2){
                ball.setDy(ball.getDy()*(-1));
                return getScore().getScore();
            }
        }
        else if(y <= ball.getY() && ball.getY() <= y+heigth){
            if(x+width+2 >= ball.getX() && ball.getX() >= x+heigth-2){
                ball.setDx(ball.getDx()*(-1));
                return getScore().getScore();
            }
            else if(x+2 >= ball.getX() && ball.getX() >= x-2){
                ball.setDx(ball.getDx()*(-1));
                return getScore().getScore();
            }
        }
        return 0;
    }

    @Override
    public void setScore(Score score) {
        this.score = score;
    }

    public Score getScore() {
        return score;
    }

    @Override
    public void onCollision(int score) {
        throw new UnsupportedOperationException("Not supported yet.");
    }
    
}
